const app = getApp()
import util from '../../../../utils/util'
import {
    SaveReport
} from '../../../../config/api'

Page({

    /**
     * 页面的初始数据
     */
    data: {
        member: '',
        formData: {
            name: '',
            mobile: '',
            idcard: ''
        },
        create_time: '',
        currDate: '',
        endDate: '2030-12-31',
        region: [],
        // customItem: '全部'
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let member = wx.getStorageSync('member');
        this.setData({
            [`formData.uname`]: member.name,
            [`formData.uid`]: member.id,
            [`formData.village_id`]: app.globalData.vollege_id
        })
        this.getYearMonth();
    },

    getYearMonth() { // 计算当前获取年月日
        var myDate = new Date();
        var tYear = myDate.getFullYear();
        var tMonth = myDate.getMonth();
        var tday = myDate.getDate()
        var m = tMonth + 1;
        if (m.toString().length == 1) {
            m = "0" + m;
        }
        if (tday.toString().length == 1) {
            tday = "0" + tday;
        }
        let currDate = tYear + '-' + m + '-' + tday
        console.log('当前日期', currDate)
        this.setData({
            currDate,
            create_time: currDate
        })
    },

    formInputChange(e) {
        const {
            field
        } = e.currentTarget.dataset
        this.setData({
            [`formData.${field}`]: e.detail.value
        })
        console.log(this.data.formData)
    },

    bindDateChange: function (e) { // 选择时间
        console.log('时间', e.detail.value)
        this.setData({
            create_time: e.detail.value
        })
    },

    bindRegionChange: function (e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
            region: e.detail.value
        })
    },

    addPerson() {
        if (!this.data.formData.name) {
            wx.showToast({
                icon: 'none',
                title: '请填写姓名',
            })
            return;
        }
        if (!(/^1[3456789]\d{9}$/.test(this.data.formData.mobile))) {
            wx.showToast({
                icon: 'none',
                title: '手机号不正确',
            })
            return;
        }
        if (!util.isCard(this.data.formData.idcard).isPass) {
            wx.showToast({
                icon: 'none',
                title: '身份证号不正确',
            })
            return;
        }
        if (!this.data.region.length) {
            wx.showToast({
                icon: 'none',
                title: '请选择地区',
            })
            return;
        }
        let param = this.data.formData;
        param['return_at'] = this.data.create_time;
        param['return_from'] = this.data.region.join('/');
        console.log(param)
        util.apiRequest(SaveReport, param).then(r => {
            console.log("添加：", r)
            if (r.data.code) {
                wx.showToast({
                    icon: 'none',
                    title: '添加成功',
                })
                wx.redirectTo({
                  url: '/pages/mine/epidemic-reporting/epidemic-reporting',
                })
            }
        })
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})